package com.star.mall.mq.comsumer;

import cn.hutool.json.JSONUtil;
import com.star.mall.constants.TopicConstant;
import com.star.mall.persistence.entity.OperationLog;
import com.star.mall.persistence.service.IOperationLogService;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@ConditionalOnProperty(
        prefix = "jms",
        name = {"mode"},
        havingValue = "rabbitmq",
        matchIfMissing = true
)
@Log4j2
public class RabbitMQConsumer {

    @Resource
    IOperationLogService operationLogService;

    @RabbitListener(queues = {TopicConstant.OPERATION_LOG})
    public void onMessage(String message) {
        OperationLog log = JSONUtil.toBean(message, OperationLog.class);
        operationLogService.save(log);
    }


}
